<template>
  <a-modal :visible="open" :width="850" :title="formTitle" @cancel="onClose">
    <a-form-model ref="form" :label-col="{ span: 7 }" :wrapper-col="{ span: 15 }" :model="form" :rules="rules">
      <a-row>
        <!-- <a-col :span="12">
          <a-form-model-item label="所属公司" prop="deptId">
            <a-input v-model="form.deptId" disabled placeholder="请输入所属公司" />
          </a-form-model-item>
        </a-col> -->
        <a-col :span="12">
          <a-form-model-item label="司机姓名" prop="driverName">
            <a-input v-model="form.driverName" placeholder="请输入司机姓名" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="手机号" prop="driverPhone">
            <a-input v-model="form.driverPhone" placeholder="请输入手机号" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="身份证号" prop="driverIdCard">
            <a-input v-model="form.driverIdCard" placeholder="请输入身份证号" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="驾驶证号" prop="driverLicenseNo">
            <a-input v-model="form.driverLicenseNo" placeholder="请输入驾驶证号" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>

        <a-col :span="12">
          <a-form-model-item label="状态" prop="status">
            <a-radio-group v-model="form.status" button-style="solid">
              <a-radio-button v-for="(d, index) in statusOptions" :key="index" :value="d.value">{{ d.label
                }}</a-radio-button>
            </a-radio-group>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="身份证正面照片" prop="driverIdCardFrontImgUrl">
            <base-upload v-model="form.driverIdCardFrontImgUrl" :limit="1"></base-upload>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="身份证反面照片" prop="driverIdCardBackImgUrl">
            <base-upload v-model="form.driverIdCardBackImgUrl" :limit="1"></base-upload>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="驾驶证照片" prop="driverLicenseImgUrl">
            <base-upload v-model="form.driverLicenseImgUrl"></base-upload>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
    <template slot="footer">
      <a-space>
        <a-button type="primary" :loading="submitLoading" @click="submitForm">
          保存
        </a-button>
        <a-button type="dashed" @click="cancel">
          取消
        </a-button>
      </a-space>
    </template>
  </a-modal>
</template>

<script>
import { getDriverInfo, addDriverInfo, updateDriverInfo } from '@/api/vehicle/driverInfo'
import baseUpload from '@/components/imgUpload/index.vue'
export default {
  name: 'CreateForm',
  props: {
    statusOptions: {
      type: Array,
      required: true
    },
  },
  components: {
    baseUpload
  },
  data() {
    return {
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        id: null,

        // deptId: null,

        driverName: null,

        driverPhone: null,

        driverIdCard: null,

        driverIdCardFrontImgUrl: null,

        driverIdCardBackImgUrl: null,

        driverLicenseNo: null,

        driverLicenseImgUrl: null,

        status: '0',

      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: '所属公司不能为空', trigger: 'blur' }
        ],
        driverName: [
          { required: true, message: '司机姓名不能为空', trigger: 'blur' }
        ],
        driverPhone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '请输入正确的手机号',
            trigger: 'blur'
          }
        ],
        driverIdCard: [
          { required: true, message: '身份证号不能为空', trigger: 'blur' }
        ],
        driverIdCardFrontImgUrl: [
          { required: true, message: '身份证正面照片不能为空', trigger: 'blur' }
        ],
        driverIdCardBackImgUrl: [
          { required: true, message: '身份证反面照片不能为空', trigger: 'blur' }
        ],
        driverLicenseImgUrl: [
          { required: true, message: '驾驶证照片不能为空', trigger: 'blur' }
        ]

      }
    }
  },
  filters: {
  },
  created() {
  },
  computed: {
  },
  watch: {
  },
  mounted() {
  },
  methods: {
    onClose() {
      this.open = false
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.formType = 1
      this.form = {
        id: null,

        // deptId: null,

        driverName: null,

        driverPhone: null,

        driverIdCard: null,

        driverIdCardFrontImgUrl: null,

        driverIdCardBackImgUrl: null,

        driverLicenseNo: null,

        driverLicenseImgUrl: null,

        status: '0'

      }
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate(row, ids) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getDriverInfo(id).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            updateDriverInfo(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addDriverInfo(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
